package D240528.filePractice;

import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class FileCountUtil {
    private static File file;
    private static int count = 0;
    private static int countLine = 0;
    private static int maxLines;
    private static int minLines = Integer.MAX_VALUE;
    private static final Map<String, String> LINES_MAP = new HashMap<>();


    public static int count(String path, String keyWord) {
        file = new File(path);
        File[] files = file.listFiles();
        if (files != null) {
            for (File value : files) {
                if (value.isDirectory()) {
                    count(value.getPath(), keyWord);
                } else if (value.getName().toLowerCase().contains(keyWord.toLowerCase())) {
                    count++;
                }
            }
        }
        return count;
    }

    public static int countLine(String path, String keyWord) throws IOException {
        file = new File(path);
        File[] files = file.listFiles();
        if (files != null) {
            for (File value : files) {
                if (value.isDirectory()) {
                    countLine(value.getPath(), keyWord);
                } else if (value.getName().toLowerCase().contains(keyWord.toLowerCase())) {
                    BufferedReader inputStream;
                    inputStream = new BufferedReader(new FileReader(value.getPath()));
                    int tempCountLine = 0;
                    while (inputStream.readLine() != null) {
                        countLine++;
                        tempCountLine++;
                    }
                    if (tempCountLine < minLines) {
                        LINES_MAP.put("MIN", value.getAbsolutePath());
                        minLines = tempCountLine;
                    } else if (tempCountLine > maxLines) {
                        LINES_MAP.put("MAX", value.getAbsolutePath());
                        maxLines = tempCountLine;
                    }
                }
            }
        }
        return countLine;
    }

    // 获取不重名文件的方法，可以采用递归的方式优化，暂时注释掉保留源代码
    /*public static void showAllDifferentName(String path, String keyWord) {
        Set<String> differentName = new HashSet<>();
        file = new File(path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File[] underFiles = files[i].listFiles();
            for (int j = 0; j < underFiles.length; j++) {
                if (underFiles[j].isDirectory()) {
                    File[] uUF = underFiles[j].listFiles();
                    for (int k = 0; k < uUF.length; k++) {
                        if (uUF[k].getName().toLowerCase().contains(keyWord.toLowerCase())) {
                            differentName.add(uUF[k].getName());
                        }
                    }
                } else if (underFiles[j].getName().toLowerCase().contains(keyWord.toLowerCase())) {
                    differentName.add(underFiles[j].getName());
                }
            }
        }
        System.out.println("\"" + keyWord + "\"类型所有文件名：" + differentName);
    }*/

    public static void main(String[] args) throws IOException {
        int count = count("src", "Java");
        System.out.println("总共" + count + "个Java文件");
        int countLine = countLine("src", "Java");
        System.out.println("总代码行数：" + countLine);
        System.out.println("平均代码行数" + countLine / count);
        System.out.println("行数最多的文件是" + LINES_MAP.get("MAX") + "(" + maxLines + "行）");
        System.out.println("行数最少的文件是" + LINES_MAP.get("MIN") + "(" + minLines + "行）");
        // showAllDifferentName("src", "md");
    }
}
